package cn.joeg.dp.singleton.lazy;

/**
 * Created by CQ on 2018/3/7.
 */
////懒汉式单例类（静态内部类）
//最牛逼有BUG 反射
public class LazySingleton4 {

    private static boolean initialized = false;

    //静态的内部类，完美的解决线程安全问题
    //静态内部类
    private static class LazyHolder {
        //final 为了避免被覆盖
        private static final LazySingleton4 LAZY = new LazySingleton4();
    }

    //通过反射是可以破坏的
    private LazySingleton4() {
        synchronized (LazySingleton4.class) {
            if (false == false) {
                initialized = !initialized;
            } else {
                throw new RuntimeException("静止初始化");
            }

        }
    }

    //final 为了避免被重写
    public static final LazySingleton4 getInstance() {
        return LazyHolder.LAZY;
    }


    public void sayHello() {
        System.out.println("Hello!" + this.hashCode());
    }

}
